package nadav.tasher.lightool.graphics.view;

import nadav.tasher.lightool.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

public class ColorPicker extends Slider {
    private int currentColor = ResourceTable.Color_white;
    private OnColorChanged onColor = null;

    public ColorPicker(Context context) {
        super(context);
        ShapeElement shapeElement=new ShapeElement();
        shapeElement.setOrientation(ShapeElement.Orientation.RIGHT_TO_LEFT);
        shapeElement.setRgbColors(new RgbColor[]{
                RgbColor.fromArgbInt(0xFF000000),
                RgbColor.fromArgbInt(0xFF0000FF),
                RgbColor.fromArgbInt(0xFF00FF00),
                RgbColor.fromArgbInt(0xFF00FFFF),
                RgbColor.fromArgbInt(0xFFFF0000),
                RgbColor.fromArgbInt(0xFFFF00FF),
                RgbColor.fromArgbInt(0xFFFFFF00),
                RgbColor.fromArgbInt(0xFFFFFFFF)
        });
        shapeElement.setCornerRadius(8);
        setBackground(shapeElement);
        setProgressElement(null);
        setMaxValue(256*7-1);
        setValueChangedListener(new ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                currentColor=getColorFromProgress(i);
                drawThumb();
                if (onColor!=null) onColor.onColorChange(currentColor);
            }
            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        drawThumb();
    }

    @Override
    public void setLayoutConfig(ComponentContainer.LayoutConfig config) {
        super.setLayoutConfig(config);
        drawThumb();
    }

    private void drawThumb() {
        int size = 0;
        if (getLayoutConfig()!= null) {
            size = getLayoutConfig().height - getPaddingTop() - getPaddingBottom();
        }
        ShapeElement element= null;
        try {
            element = (ShapeElement) Utils.getSizedCoaster(getResourceManager().getElement(ResourceTable.Color_white).getColor(),16,10,size/2,size);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        ShapeElement element1= (ShapeElement) Utils.getSizedCoaster(currentColor,16,10, (int) (size / 2.25), (int) (size / 1.25));
        setThumbElement(element1);
        setThumbElement(element);

        }

   private int getProgressFromColor(int color){
        int progress=0;
        for (int i=0;i<=getMax();i++){
            if (getColorFromProgress(i)==color) progress=i;
        }
        return progress;
   }


    private int getColorFromProgress(int progress) {
        int r = 0;
        int g = 0;
        int b = 0;
        if (progress < 256) {
            b = progress;
        } else if (progress < 256 * 2) {
            g = progress % 256;
            b = 256 - progress % 256;
        } else if (progress < 256 * 3) {
            g = 255;
            b = progress % 256;
        } else if (progress < 256 * 4) {
            r = progress % 256;
            g = 256 - progress % 256;
            b = 256 - progress % 256;
        } else if (progress < 256 * 5) {
            r = 255;
            g = 0;
            b = progress % 256;
        } else if (progress < 256 * 6) {
            r = 255;
            g = progress % 256;
            b = 256 - progress % 256;
        } else if (progress < 256 * 7) {
            r = 255;
            g = 255;
            b = progress % 256;
        }
        return Color.rgb(r,g,b);
    }



   public interface OnColorChanged {
        void onColorChange(int color);
    }
}
